package com.android.job.service.request;

import java.util.ArrayList;
import org.apache.http.message.BasicNameValuePair;
import android.os.Handler;
import android.util.Log;
import com.android.job.fusion.FusionCode;
import com.android.job.model.LoginInfo;
import com.android.job.model.PostInfo;
import com.android.job.service.ConnectionTask;
import com.android.job.service.JSONRequest;
import com.android.job.util.CommonUtil;
import com.android.job.util.json.JSONArray;
import com.android.job.util.json.JSONException;
import com.android.job.util.json.JSONObject;

/**
 * 登录请求
 * 
 * @author
 * 
 */
public class LoginRequest extends JSONRequest
{
	private String userName;
	private String userPassword;

	public LoginRequest(Handler handler, String httpUrl)
	{
		super(handler, httpUrl);
		setHttpType(ConnectionTask.POST);
	}

	public void setValues(String userName, String userPassword)
	{
		this.userName = userName;
		this.userPassword = userPassword;
	}

	@Override
	protected boolean parseJSONResponse(JSONObject json)
	{
		Log.i("LoginRequest", "===response===json======" + json.toString());
		if (!json.isNull("jsonData"))
		{
			JSONObject jsonObject;
			try
			{
				jsonObject = json.getJSONObject("jsonData");
				LoginInfo info = new LoginInfo();
				info.postList = new ArrayList<PostInfo>();

				if (!jsonObject.isNull("success"))
				{
					info.success = jsonObject.getString("success");
				}
				if (!jsonObject.isNull("userType"))
				{
					info.userType = jsonObject.getString("userType");
				}
				if (!jsonObject.isNull("userId"))
				{
					info.userId = jsonObject.getString("userId");
				}
				if(!jsonObject.isNull("realName"))
				{
					info.realName = jsonObject.getString("realName");
				}
				if (!jsonObject.isNull("postArr"))
				{
				
					JSONArray postArr = jsonObject.getJSONArray("postArr");
					info.json = postArr.toString();
					PostInfo pInfo;
					for (int i = 0; i < postArr.length(); i++)
					{
						JSONObject data = postArr.getJSONObject(i);
						pInfo = new PostInfo();
						pInfo.postId = data.getString("postId");
						pInfo.postName = data.getString("postName");
						info.postList.add(pInfo);
					}
				}

				CommonUtil.sendMessage(handler, FusionCode.RETURN_JSONOBJECT, info);
			}
			catch (JSONException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			

		}

		return true;
	}

	@Override
	protected void appendMainBody()
	{
		this.paramsters = new ArrayList();
		JSONObject userJson;
		try
		{
			jsonObject = new JSONObject();
			userJson = new JSONObject();

			userJson.put("userName", userName);
			userJson.put("passWord", userPassword);

			jsonObject.put("requestType", "login");
			jsonObject.put("jsonData", userJson);
			paramsters.add(new BasicNameValuePair("jsonData", jsonObject
					.toString()));
			Log.i("LoginRequest",
					"===request===json======" + jsonObject.toString());
		}
		catch (JSONException e)
		{
			e.printStackTrace();
		}
	}

}
